package rbac

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"store/logic"
	"store/models/rbac"
)

type AdminRightsLogic struct {
	logic.RestLogic
	Ctx *gin.Context
}

type AdminRightsInput struct {
	logic.XmlInput
	Range       uint8  `json:"range" xml:"range" form:"range" field:"range" binding:"min=0" msg:"排序必须是非负数"`
	IsShow      uint8  `json:"isShow" xml:"isShow" form:"isShow" field:"is_show" binding:"min=0" msg:"必须是非负数"`
	Id          uint64 `json:"id" xml:"id" form:"id" field:"id" binding:"min=0" msg:"权限表必须是非负数"`
	Tag         string `json:"tag" xml:"tag" form:"tag" field:"tag" binding:"max=64" msg:"tag最大长度为64"`
	Description string `json:"description" xml:"description" form:"description" field:"description" binding:"max=32" msg:"最大长度为32"`
	IsOn        uint8  `json:"isOn" xml:"isOn" form:"isOn" field:"is_on" binding:"min=0" msg:"是否启用必须是非负数"`
	Name        string `json:"name" xml:"name" form:"name" field:"name" binding:"max=64" msg:"权限名称最大长度为64"`
	Level       uint8  `json:"level" xml:"level" form:"level" field:"level" binding:"min=0" msg:"等级必须是非负数"`
	ParentId    uint64 `json:"parentId" xml:"parentId" form:"parentId" field:"parent_id" binding:"min=0" msg:"父id必须是非负数"`
}

/**克隆logic,用于存放*gin.Context
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminRightsLogic) Clone(ctx *gin.Context) logic.RestLogic {
	newLogic := *this
	newLogic.Ctx = ctx
	return &newLogic
}

/**解析http输入
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminRightsLogic) LoadInput() (input interface{}, err error) {
	in := AdminRightsInput{
		IsOn: 1,
	}
	err = this.Ctx.ShouldBind(&in)
	return in, err
}

/**解析http输入转化为update输入
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminRightsLogic) LoadInput4Update(where map[string]interface{}) (input interface{}, version uint64, err error) {
	model, ok := this.View(where)
	if !ok {
		err = fmt.Errorf("not found")
		return
	}

	val := model.(rbac.AdminRights)
	value := AdminRightsInfo2Input(&val)
	err = this.Ctx.ShouldBind(&value)
	input = value
	version = val.Version
	return
}

/**创建输入
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminRightsLogic) NewCreateInput() interface{} {
	return AdminRightsInput{
		IsOn: 1,
	}
}

/**数据库Model转Input
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminRightsInfo2Input(info *rbac.AdminRights) AdminRightsInput {
	return AdminRightsInput{
		Description: info.Description,
		Range:       info.Range,
		IsShow:      info.IsShow,
		Id:          info.Id,
		Tag:         info.Tag,
		ParentId:    info.ParentId,
		IsOn:        info.IsOn,
		Name:        info.Name,
		Level:       info.Level,
	}
}

/**插入
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminRightsLogic) Add(input interface{}) (id uint64) {
	in := input.(*interface{})
	info := logic.Input2Map((*in).(AdminRightsInput))
	id = rbac.AdminRightsCreate(this.Ctx, info)
	if id > 0 {
		ResetAllEnableRights(this.Ctx)
	}
	return id
}

/**删除
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminRightsLogic) Delete(id uint64) (rows int64) {
	rows = rbac.AdminRightsDelete(this.Ctx, id)
	if rows > 0 {
		ResetAllEnableRights(this.Ctx)
	}
	return rows
}

/**修改
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminRightsLogic) Update(version uint64, input interface{}) (rows int64) {
	infoInput := input.(AdminRightsInput)
	info := logic.Input2Map(infoInput)
	rows = rbac.AdminRightsUpdate(this.Ctx, infoInput.Id, version, info)
	if rows > 0 {
		ResetAllEnableRights(this.Ctx)
	}
	return rows
}

/**启用禁用
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminRightsLogic) UpOrDown(id uint64, value uint8) (rows int64) {
	rows = rbac.AdminRightsUpOrDown(this.Ctx, id, value)
	if rows > 0 {
		ResetAllEnableRights(this.Ctx)
	}
	return rows
}

/**详情
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminRightsLogic) View(where map[string]interface{}) (info interface{}, exist bool) {
	model := rbac.AdminRightsInfo(this.Ctx, where)
	return model, model.Id > 0
}

/**列表分页检索
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminRightsLogic) Index(page int64, pageSize int64, where map[string]interface{}, orders []string) (pageData map[string]interface{}) {
	return rbac.AdminRightsIndex(this.Ctx, page, pageSize, where, orders)
}
